<?php
/**
 * Create by 11li
 * Date 2020/2/26
 * Time 14:50
 *
 */

namespace app\common\service;


use app\common\library\Kdniao;
use app\common\model\OrderShipping;
use think\Cache;

class ShippingService extends BaseService
{
	public function __construct()
	{
		$this->model = new OrderShipping();
	}
	
	public function getOrderShipping($orderId)
	{
		$shippingList = $this->model->getListByOrderId($orderId);
		
		if($shippingList) {
			foreach ($shippingList as &$item) {
				$item['detail'] = $this->getShippinDetail($item['code'], $item['shipping_no']);
			}
		}
		return $shippingList;
	}
	
	private function getShippinDetail($code, $shipping_no)
	{
		
		$key = md5($code.$shipping_no);
		if(!$list = Cache::get($key)) {
			$kdniao = Kdniao::getInstance();
			$res = $kdniao->getOrderTracesByJson($code, $shipping_no);
			
			$list = [];
			
			if(!empty($res['Traces'])) {
				foreach ($res['Traces'] as $item) {
					$list[] = [
						'title' => $item['AcceptTime'],
						'desc' => $item['AcceptStation']
					];
				}
			}
			$list = array_reverse($list);
			Cache::set($key, $list);
		}
		
		return $list;
	}
}